<?php
/**
 * 时间日期常用
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.helpers.utils
 */
class CDateUtil {
	
	/**
	 * 转变时间格式
	 * Enter description here ...
	 * @param unknown_type $format
	 * @param unknown_type $time
	 */
	public function changeFormat($time, $format = 'Y-m-d') {
		return date($format, strtotime($time));
	}
	
	/**
	 * 转换成中文星期
	 * Enter description here ...
	 * @param int $time
	 */
	public function chineseWeek($time) {
		$week = '星期';
		switch (date('N',$time)) {
			case 1:
				$week .= '一';	break;
			case 2:
				$week .= '二';	break;
			case 3:
				$week .= '三';	break;
			case 4:
				$week .= '四';	break;
			case 5:
				$week .= '五';	break;
			case 6:
				$week .= '六';	break;
			case 7:
				$week .= '日';	break;
			default:
				break;
		}
		return $week;
	}
	
	/**
	 * 两个时间的差值
	 * Enter description here ...
	 * @param string $date1
	 * @param string $date2
	 */
	public function dateDiff($date1, $date2) {
		$time = strtotime($date1) - strtotime($date2);
		$days = floor($time / (3600 * 24));
		$str = '';
		if($days>0) {
			$time = $time % (3600 * 24);
			$str .= "{$days}天";
		}
		$hours = floor($time / 3600);
		if($hours>0) {
			$time = $time % 3600;
			$str .= "{$hours}小时";
		}
		$minutes = intval($time/60);
		if($minutes>0) {
			$str .= "{$minutes}分钟";
		}
		return $str;
	}
	
	/**
	 * 获取中文描述的时间
	 * 比如：3天5小时16分钟
	 * @param int $time
	 */
	public function getChDate($time) {
		$days = floor($time / (3600 * 24));
		$str = '';
		if($days>0) {
			$time = $time % (3600 * 24);
			$str .= "{$days}天";
		}
		$hours = floor($time / 3600);
		if($hours>0) {
			$time = $time % 3600;
			$str .= "{$hours}小时";
		}
		$minutes = intval($time/60);
		if($minutes>0) {
			$str .= "{$minutes}分钟";
		}
		return $str;
	}
	
	public function calDaysInMonth($calendar, $month, $year) {
		if (!defined('CAL_GREGORIAN')) {
			define('CAL_GREGORIAN', 1);
		}
		if (!function_exists('cal_days_in_month')) {
			function cal_days_in_month($calendar, $month, $year) {
				return date('t', mktime(0, 0, 0, $month, 1, $year));
			}
		}
		return cal_days_in_month($calendar, $month, $year);
	}
	

}

?>